package com.elcom.roaming.module.synthesis;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.elcom.roaming.common.config.ConfigModule;
import com.elcom.roaming.common.db.DBConfig;
import com.elcom.roaming.common.db.cache.CacheInsert;
import com.elcom.roaming.common.server.SMPPServer;
import com.elcom.roaming.module.synthesis.isup.ISUPMatchingObjects;
import com.elcom.roaming.module.synthesis.lb.OutputService;
import com.elcom.roaming.module.synthesis.match.MSRNMatchingObjects;
import com.elcom.roaming.module.synthesis.pr.PRMatchingObjects;

public class ModuleSynthesisInit extends MonitorApp{

	private Logger logger;
	private Logger loggerMonitor;
	private Logger loggerOutput;
	private Logger loggerDB;
	private Logger loggerSMPP;
	
	private OutputService output;
	private MSRNMatchingObjects msrnMatching;
	private ISUPMatchingObjects isupMatching;
	private PRMatchingObjects prMatching;
	private CacheInsert cacheInsert;
	private DBConfig dbConfig;
	private SMPPServer server;

	public ModuleSynthesisInit() {
		logger = LoggerFactory.getLogger(ConfigModule.LOG_DEBUG_FLOW);
		loggerMonitor = LoggerFactory.getLogger(ConfigModule.LOG_DEBUG_MONITOR);
		loggerOutput = LoggerFactory.getLogger(ConfigModule.LOG_DEBUG_LB_OUTPUT);
		loggerDB = LoggerFactory.getLogger(ConfigModule.LOG_DEBUG_DB);
		loggerSMPP = LoggerFactory.getLogger(ConfigModule.LOG_DEBUG_SMPP_SERVER);
		logger.info("Module Synthesis starting...");
		logger.info("DBConfig init...");
		dbConfig = new DBConfig(loggerDB);

		logger.info("CacheInsert init...");
		cacheInsert = new CacheInsert(loggerDB, "Cache-Insert", dbConfig.getDBQuery());

		logger.info("LB Clients init...");
		output = new OutputService(loggerOutput);
		
		logger.info("MSRN Matching init...");
		msrnMatching = new MSRNMatchingObjects(logger, "MSRN-Matching", cacheInsert, dbConfig.getDBQuery(), output);
		
		logger.info("ISUP Matching init...");
		isupMatching = new ISUPMatchingObjects(logger, "ISUP-Matching", msrnMatching);

		logger.info("PRN Matching init...");
		prMatching = new PRMatchingObjects(logger, "PRN-Matching", msrnMatching);

		logger.info("SMPP Server init...");
		server = new SMPPServer(loggerSMPP, "SMPP-Server-Synthesis",
				ConfigModule.MODULE_CONFIG_SMPP_SERVER_FILE, isupMatching, prMatching);
	}
	
	public void run(){
		try {
			setMonitorLogger(loggerMonitor);
			initMonitor();
			monitorDatas();
			runMonitor();
		} catch (IOException e) {
			logger.error("Monitor init error.", e);
			return;
		}
		
		logger.info("Create LB Client...");
		output.startClients();
		
		new Thread(cacheInsert, cacheInsert.getName()).start();
		
		new Thread(isupMatching, isupMatching.getName()).start();
		
		new Thread(prMatching, prMatching.getName()).start();
		
		try {
			server.start();
		} catch (Throwable e) {
			logger.error("SMPP Server start error.", e);
			return;
		}
		
	}

	private void monitorDatas() {
		isupMatching.setQueueName("ISUPQueue");
		addQueue(isupMatching.getQueue(), isupMatching.getQueueName(), ConfigModule.MATCHING_MAX_QUEUE_SIZE);
		
		isupMatching.setMapName("ISUPMap");
		addMap(isupMatching.getMap(), isupMatching.getMapName(), ConfigModule.MATCHING_MAX_MAP_SIZE);
		
		prMatching.setQueueName("PRNQueue");
		addQueue(prMatching.getQueue(), prMatching.getQueueName(), ConfigModule.MATCHING_MAX_QUEUE_SIZE);
		
		prMatching.setMapName("PRNPMap");
		addMap(prMatching.getMap(), prMatching.getMapName(), ConfigModule.MATCHING_MAX_MAP_SIZE);
		
		msrnMatching.setMapName("MSRNMap");
		addMap(msrnMatching.getMap(), msrnMatching.getMapName(), ConfigModule.MATCHING_MAX_MSRN_MAP_SIZE);
		
		cacheInsert.setQueueName("CacheQueue");
		addQueue(cacheInsert.getQueue(), cacheInsert.getQueueName(), ConfigModule.DB_MAX_QUEUE_SIZE);
		
		output.setQueueName("LBOutputQueue");
		addQueue(output.getQueue(), output.getQueueName(), ConfigModule.LB_CLIENT_MAX_QUEUE_SIZE);
		
	}

}
